package com.ruoyi.web.controller.oto;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.CollectionModle;
import com.ruoyi.oto.constants.CompanyShowFlag;
import com.ruoyi.oto.constants.CompanyStatus;
import com.ruoyi.oto.constants.DeleteFlag;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.CompanyType;
import com.ruoyi.oto.domain.ModelCollection;
import com.ruoyi.oto.domain.dto.CompanyCheckDTO;
import com.ruoyi.oto.domain.dto.CompanyQuryDTO;
import com.ruoyi.oto.domain.dto.CompanyRegDTO;
import com.ruoyi.oto.service.CompanyCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 后台-企业管理
 */
@RestController
@RequestMapping("/adm/company")
public class CompanyController extends BaseController {

    /**
     * 企业服务
     */
    @Autowired
    CompanyCoreService companyCoreService;

    /**
     * 分页获取企业  OK
     * 用在企业搜索
     * 待审核的企业搜索
     * @param quryDTO
     * @return
     */
    @PostMapping("/companyList")
    public AjaxResult<IPage<CompanyCore>> companyList(@RequestBody CompanyQuryDTO quryDTO) {
        quryDTO.setShowFlag(CompanyShowFlag.显示);
        quryDTO.setStatus(CompanyStatus.审核通过);
        IPage<CompanyCore> data =  companyCoreService.pageCompany(quryDTO);
        return AjaxResult.success(data);
    }

    /**
     * 企业类型列表 Ok
     * @return
     */
    @GetMapping("companyTypeList")
    public AjaxResult<List<CompanyType>> companyTypeList() {
        CompanyType companyType = new CompanyType();
        List<CompanyType> list = companyType.selectList(new LambdaQueryWrapper<CompanyType>().eq(CompanyType::getDelFlag, DeleteFlag.NO));
        return AjaxResult.success(list);
    }

    /**
     * 企业详情
     * @param companyId 企业ID
     * @return
     */
    @GetMapping("companyDetail/{companyId}")
    public AjaxResult<CompanyCore> companyDetail(@PathVariable Long companyId) {
        CompanyCore companyCore = new CompanyCore().selectById(companyId);
        //获取企业的产品和标签
        //TODO 增加产品和标签的搜索
        return AjaxResult.success(companyCore);
    }


    /**
     * 编辑企业
     * @param companyRegDTO 企业ID
     * @return
     */
    @PostMapping("editCompany")
    public AjaxResult<Long> editCompany(@Valid @RequestBody CompanyRegDTO companyRegDTO) {
        if(companyRegDTO.getCompanyId()==null){
            throw new ServiceException("企业ID不能为空");
        }
        if(this.getLoginUser().getUser().isAdmin()){
            Long companyId = companyCoreService.editCompany(this.getLoginUser(),companyRegDTO);
            return AjaxResult.success(companyId);
        }
        else{
            return AjaxResult.error("您没有编辑企业的权限");
        }

    }

    /**
     * 删除企业
     * @param companyId 企业ID
     * @return
     */
    @GetMapping("deleteCompany/{companyId}")
    public AjaxResult<Long> deleteCompany(@PathVariable Long companyId) {
        companyCoreService.deleteCompany(this.getLoginUser(),companyId);
        return AjaxResult.success();
    }

    /**
     * 分页获取待审核的企业 OK
     * @param quryDTO
     * @return
     */
    @PostMapping("/companyCheckList")
    public AjaxResult<IPage<CompanyCore>> companyCheckList(@RequestBody CompanyQuryDTO quryDTO) {
        quryDTO.setStatus(CompanyStatus.待审核状态);
        IPage<CompanyCore> data =  companyCoreService.pageCompany(quryDTO);
        return AjaxResult.success(data);
    }

    /**
     * 后台审核企业  OK
     * @param checkDTO
     * @return
     */
    @PostMapping("/checkCompany")
    public AjaxResult<Long> checkCompany(@RequestBody CompanyCheckDTO checkDTO) {
        Long companyId = companyCoreService.checkCompany(this.getLoginUser(),checkDTO);
        return AjaxResult.success(companyId);
    }

}
